package LeetCode.month01.day18;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class No416 {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < nums.length; i++) {
            sum+=nums[i];
            max = Math.max(max,nums[i]);
        }
        if(sum%2==1) return false;
        int target = sum/2;
        if(max>target) return false;
        Set<Integer> set = new HashSet<>();
        boolean[][] dp = new boolean[nums.length][target+1];
        for (int i = 0; i < nums.length; i++) {
            dp[i][0]=true;
        }
        dp[0][nums[0]]=true;
        for (int i = 1; i <nums.length ; i++) {
            for (int j = 1; j <=target ; j++) {
                if(j>=nums[i]){
                    dp[i][j]=dp[i-1][j]||dp[i-1][j-nums[i]];
                }else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[nums.length-1][target];
    }
}
